package com.lykj.llw.box.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.dinuscxj.itemdecoration.LinearDividerItemDecoration
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.R
import com.lykj.llw.box.adapter.HotGameLookMoreAdapter
import com.lykj.llw.box.adapter.home.HomeHotGameAdapter
import com.lykj.llw.box.data.bean.response.HomeResponse
import com.lykj.llw.box.data.bean.response.HotGameLookMoreResponse
import com.lykj.llw.box.databinding.ActHotGameLookMoreBinding
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 热门游戏查看更多
 * ================================================
 *
 * @author gosha
 * @date 2023/12/8-17:22
 */
class HotGameLookMoreActivity : BaseActivity() {
    lateinit var binding: ActHotGameLookMoreBinding

    //精选游戏
    private val mHotGameAdapter = HotGameLookMoreAdapter()
    private val mHotGames = ArrayList<HotGameLookMoreResponse.DataDTO.RowsDTO>()

    private var currentPage = 1
    private val pageSize = 10
    private var totalPage = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActHotGameLookMoreBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initUI()
        initData()
        setListener()
    }

    override fun initUI() {
        binding.titleBar.tvTitle.text = "精选游戏"
        binding.titleBar.tvRightOption.visibility = View.GONE

        initRCV()
    }

    private fun initRCV() {
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rcvHotGame.layoutManager = linearLayoutManager
        binding.rcvHotGame.setHasFixedSize(true)

        val dividerItemDecoration = LinearDividerItemDecoration(
            this, LinearDividerItemDecoration.LINEAR_DIVIDER_VERTICAL
        )
        dividerItemDecoration.setDivider(
            resources.getDrawable(
                R.drawable.divider_15dp, resources.newTheme()
            )
        )
        binding.rcvHotGame.addItemDecoration(dividerItemDecoration, 0)
        mHotGameAdapter.data = mHotGames
        mHotGameAdapter.setOnItemClickListener { _, _, position ->
            val intent = Intent(this, GameDetailActivity::class.java)
            intent.putExtra("gid", mHotGames[position].gid)
            startActivity(intent)
        }
        val emptyView = View.inflate(this, R.layout.empty_playing, null)
        emptyView.findViewById<TextView>(R.id.tv_desc).text = "暂无数据"
        mHotGameAdapter.setEmptyView(emptyView)
        binding.rcvHotGame.adapter = mHotGameAdapter
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this)
            .transparentBar()
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    //获取热门游戏列表
    private fun obtainHotGameList() {
        clearBodyParam()
        initBodyParam()
        bodyParams["pageNo"] = currentPage
        bodyParams["pageSize"] = pageSize
        RetrofitManager.instance.obtainHotGameLookMore(bodyParams, object :
            CustomResponseBodyObserver<ResponseBody>() {
            override fun onSubscribe(d: Disposable) {
                addDisposables(this@HotGameLookMoreActivity, d)
            }

            override fun specificHandle(responseBody: String) {
                val bean = GsonUtils.fromJson(responseBody, HotGameLookMoreResponse::class.java)

                totalPage = bean?.data?.totalPage!!
                if (!bean.data?.rows.isNullOrEmpty()) {
                    mHotGames.addAll(bean.data?.rows!!)
                }
                mHotGameAdapter.notifyDataSetChanged()
            }

            override fun handleException(ret: String, msg: String?) {
                msg?.let {
                    ToastUtils.showShort(this@HotGameLookMoreActivity, it)
                }
            }
        })
    }

    override fun initData() {
        obtainHotGameList()
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }
        binding.srfHotGame.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                //获取精选热门
                currentPage = 1
                mHotGames.clear()
                mHotGameAdapter.notifyDataSetChanged()

                obtainHotGameList()

                refreshLayout.finishRefresh(500, true, false)
                refreshLayout.resetNoMoreData()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (currentPage == totalPage) {
                    refreshLayout.finishLoadMoreWithNoMoreData()
                    ToastUtils.showShort(this@HotGameLookMoreActivity, "没有更多数据了")
                    return
                }
                refreshLayout.finishLoadMore(500, true, false)
                currentPage++

                obtainHotGameList()
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}